package org.hackystat.sdsa.analyzer.control.processor;

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertTrue;

import java.util.logging.Logger;

import javax.xml.datatype.XMLGregorianCalendar;

import org.hackystat.sdsa.analyzer.model.activity.Activity;
import org.hackystat.sdsa.analyzer.model.activity.BufferTransActivity;
import org.hackystat.sensorbase.resource.sensordata.jaxb.Properties;
import org.hackystat.sensorbase.resource.sensordata.jaxb.Property;
import org.hackystat.sensorbase.resource.sensordata.jaxb.SensorData;
import org.hackystat.utilities.tstamp.Tstamp;
import org.junit.Before;
import org.junit.Test;

/**
 * Test the BuffTrasProcessor.
 * 
 * @author hongbing
 *
 */
public class TestBuffTransActivityProcessor {
  /** BuffTrans activity processor */
  private BuffTransActivityProcessor processor;
  private Logger logger;
  
  /**
   * Prepares test variables.
   * 
   * @throws Exception If error in testing. 
   */
  @Before public void setUp() throws Exception {
    this.processor = new BuffTransActivityProcessor();
    this.logger = Logger.getLogger(TestBuffTransActivityProcessor.class.getName());
  }
  
  /**
   * Test the conversion of buff trans activity.
   * 
   * @throws Exception If any error in testing.
   */
  @Test public void testBuffTrans() throws Exception {
    /** BuffTrans data. */
    SensorData buffTransData = new SensorData();
    XMLGregorianCalendar tstamp = Tstamp.makeTimestamp("2007-11-17T10:25:16.949-08:00");
    buffTransData.setTimestamp(tstamp);
    buffTransData.setResource("file:/C:/cvs/work/com/foo/TestBar.java");
    buffTransData.setSensorDataType("DevEvent");
    
    // Add property
    Properties props = new Properties();
    buffTransData.setProperties(props);

    Property type = new Property();
    type.setKey("Type");
    type.setValue("Edit");
    props.getProperty().add(type);
    
    Property subType = new Property();
    subType.setKey("Subtype");
    subType.setValue("BufferTransition");
    props.getProperty().add(subType);

    Property fromBuff = new Property();
    fromBuff.setKey(BufferTransActivity.FROM_BUFF_NAME);
    fromBuff.setValue("file:/C:/cvs/work/com/foo/Bar.java");
    props.getProperty().add(fromBuff);

    Property toBuff = new Property();
    toBuff.setKey(BufferTransActivity.TO_BUFF_NAME);
    toBuff.setValue("file:/C:/cvs/work/com/foo/TestBar.java");
    props.getProperty().add(toBuff);

    EditActivityProcessor editProcessor = new EditActivityProcessor();
    assertFalse("Test processable of document edit activity", 
        editProcessor.processable(buffTransData));

    assertTrue("Test processable of buffer transition activity", 
           this.processor.processable(buffTransData));
    
    Activity activity = this.processor.process(buffTransData, this.logger);
    assertTrue("Test return type", activity instanceof BufferTransActivity);

    BufferTransActivity buffTransActivity = (BufferTransActivity) activity;
    assertEquals("Test from file", "file:/C:/cvs/work/com/foo/Bar.java", 
        buffTransActivity.getFromResource());
    assertEquals("Test to file", "TestBar.java", buffTransActivity.getToFile());
  }
}
